package com.shouhe.mysap.controller.tech;


import com.shouhe.mysap.common.MySapResult;
import com.shouhe.mysap.common.PageResult;
import com.shouhe.mysap.entity.SaleItem;
import com.shouhe.mysap.entity.TechBom;
import com.shouhe.mysap.service.SaleItemService;
import com.shouhe.mysap.service.TechBomService;
import com.shouhe.mysap.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 守鹤
 * @since 2020-06-02
 */
@RestController
@RequestMapping("/tech-bom")
public class TechBomController {

    @Autowired
    private TechBomService techBomService;

    @Autowired
    private SaleItemService saleItemService;


    /**
     * @描述 得到全部的技术Bom
     * @参数
     * @返回值
     */
    @RequestMapping("/getTechBoms")
    public PageResult getTechBoms(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows, String itemCode, String itemName) {
        Map<String, Object> map = new HashMap<>();
        map.put("item_code", itemCode);
        map.put("item_name", itemName);
        PageResult pageResult = PageResultUtil.getPageResultAllEq(page, rows, techBomService, map);
        return pageResult;
    }

    /**
     * @描述 得到全部的技术Bom
     * @参数
     * @返回值
     */
    @RequestMapping("/lookTechBomsByItemCode")
    public PageResult lookTechBomsByItemCode(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows, String itemCode) {
        PageResult pageResult = PageResultUtil.getPageResultEq(page, rows, techBomService, "item_code", itemCode);
        return pageResult;
    }


    /**
     * @描述 添加一个技术Bom
     * @参数
     * @返回值
     */
    @RequestMapping("/addTechBom")
    public MySapResult addTechBom(TechBom techBom) {
        //添加一个Bom信息
        MySapResult result = AddUtil.addObject("添加技术Bom", techBom, techBomService);
        if(!result.isSuccess()){
            return MySapResult.fail("添加技术Bom失败");
        }
        MySapResult updateResult=null;
        //价格
        Double price =techBom.getThingPrice();
        Double sum=-1d;
        //前端输入了价格
        if(price!=null){
            sum = SelectUtil.selectSaleItemPrice(techBom.getItemCode(), techBomService);
        }
        //得到商品
        SaleItem saleItem = SelectUtil.selectOneEq("item_code", techBom.getItemCode(), saleItemService);
        updateResult=UpdateUtil.updateObjectById("修改商品价格",saleItem.setBomPrice(sum),saleItemService);
        if(updateResult.isSuccess()){
            return result;
        }
        return updateResult;
    }

    /**
     * @描述 通过 id数组 删除技术Bom
     * @参数
     * @返回值
     */
    @RequestMapping("/deleteTechBoms")
    public MySapResult deleteTechBoms(@RequestParam("ids[]") Integer[] ids) {
        List<TechBom> boms = SelectUtil.selectListIn("id", Arrays.asList(ids), techBomService);

        MySapResult result = DeleteUtil.deleteMany("删除技术Bom", ids, techBomService);
        if(!result.isSuccess()){
            return MySapResult.fail("删除技术Bom失败");
        }
        MySapResult updateResult=null;
        //将商品的钱 减掉
        //总价
        Double sum=0d;
        for (TechBom bom : boms) {
            sum+=bom.getThingPrice()*bom.getUseCnt();
        }

        //查看商品是否有价格
        TechBom techBom = boms.get(0);
        String itemCode = techBom.getItemCode();
        SaleItem saleItem = SelectUtil.selectOneEq("item_code", itemCode, saleItemService);
        if(saleItem!=null){
            Double bomPrice = saleItem.getBomPrice();
            //商品有价格
            if(bomPrice!=-1){
                bomPrice-=sum;
                //新价格
                saleItem.setBomPrice(bomPrice);
            }
            updateResult= UpdateUtil.updateObjectById("Bom删除，商品的价格减少", saleItem, saleItemService);
            if(updateResult.isSuccess()){
                return result;
            }
        }
        return updateResult;
    }

    /**
     * @描述 更新技术Bom
     * @参数
     * @返回值
     */
    @RequestMapping("/updateTechBom")
    public MySapResult updateTechBom(TechBom techBom) {
        MySapResult result = UpdateUtil.updateObjectById("更新技术Bom", techBom, techBomService);
        if(!result.isSuccess()){
            return MySapResult.fail("更新技术Bom失败");
        }
        MySapResult updateResult=null;
        //价格
        Double price =techBom.getThingPrice();
        Double sum=-1d;
        //前端输入了价格
        if(price!=null){
            sum = SelectUtil.selectSaleItemPrice(techBom.getItemCode(), techBomService);
        }
        //得到商品
        SaleItem saleItem = SelectUtil.selectOneEq("item_code", techBom.getItemCode(), saleItemService);
        updateResult=UpdateUtil.updateObjectById("修改商品价格",saleItem.setBomPrice(sum),saleItemService);
        if(updateResult.isSuccess()){
            return result;
        }
        return updateResult;
    }


}

